package com.monkey.demolist.tree.util;

import android.util.Log;

import com.monkey.demolist.R;
import com.monkey.demolist.tree.annotation.TreeNodeId;
import com.monkey.demolist.tree.annotation.TreeNodeLabel;
import com.monkey.demolist.tree.annotation.TreeNodePid;
import com.monkey.demolist.tree.bean.Node;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Author: Archur
 * Date: 2017-10-20
 * Time: 11:16
 */


public class TreeHelper {


    /**
     *
     * @param datas
     * @param <T>
     * @return
     */
    private static <T> List<Node> convertDatas2Notes(List<T> datas) throws IllegalAccessException {

        List<Node> nodes = new ArrayList<>();
        Node node = null;

        // 1 根据反射和注解把数据Bean转换成我们定义好的Node
        for (T t : datas) {

            int id = -1;
            int pid = -1;
            String label = null;

            Class<?> clazz = t.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field: fields) {
                if (field.getAnnotation(TreeNodeId.class) != null){
                    field.setAccessible(true);
                    TreeNodeId annotation = field.getAnnotation(TreeNodeId.class);
                    Class type = annotation.type();
                    if (type == String.class){

                    }else if (type == Integer.class){
                        id = field.getInt(t);
                    }
                }
                if (field.getAnnotation(TreeNodePid.class) != null){
                    field.setAccessible(true);
                    pid = field.getInt(t);
                }
                if (field.getAnnotation(TreeNodeLabel.class) != null){
                    field.setAccessible(true);
                    label = (String) field.get(t);
                }
            }

            node = new Node(id,pid,label);
            nodes.add(node);
        }

        // 2 设置node之间的关系
        for (int i = 0; i < nodes.size(); i++) {
            Node n = nodes.get(i);
            for (int j = i+1; j < nodes.size(); j++) {
                Node m = nodes.get(j);
                if (m.getpId() == n.getId()){
                    n.getChildren().add(m);
                    m.setParent(n);
                }else if(m.getId() == n.getpId()){
                    m.getChildren().add(n);
                    n.setParent(m);
                }
            }
        }

        // 3 设置图标
        for (Node n : nodes) {
            setNodeIcon(n);
        }

        return nodes;
    }

    public static <T> List<Node> getSortedNodes(List<T> datas,int defaultExpandLevel) throws IllegalAccessException {


        List<Node> result = new ArrayList<>();
        Log.e("TAG", "getSortedNodes:" + datas.size());

        List<Node> nodes = convertDatas2Notes(datas);

        Log.e("TAG", "getSortedNodes:" + nodes.size());

        List<Node> rootNodes = getRootNodes(nodes);

        Log.e("TAG", "getSortedNodes:" + rootNodes.size());
        for (Node node : rootNodes) {
            addNode(result,node,defaultExpandLevel,1);
        }

        Log.e("TAG", "getSortedNodes:" + result.size());

        return result;
    }

    /**
     * 把某个节点的所有子节点放进去
     * @param result
     * @param node
     * @param defaultExpandLevel
     * @param currentLevel
     */
    private static void addNode(List<Node> result, Node node, int defaultExpandLevel, int currentLevel) {

        result.add(node);
        if (defaultExpandLevel >= currentLevel){
            node.setExpand(true);
        }
        if (node.isLeaf()) return;

        for (int i = 0; i < node.getChildren().size(); i++) {
            addNode(result,node.getChildren().get(i),defaultExpandLevel,currentLevel+1);
        }
    }

    /**
     * 过滤不可见的node
     * @param nodes
     * @return
     */
    public static List<Node> filterVisibleNodes(List<Node> nodes) {
        List<Node> last = new ArrayList<>();
        for (Node n : nodes) {
            if (n.isRoot() || n.isParentExpand()){
                setNodeIcon(n);
                last.add(n);
            }
        }
        return last;
    }

    /**
     * 取出所有的根节点
     * @param nodes
     * @return
     */
    private static List<Node> getRootNodes(List<Node> nodes) {

        List<Node> root = new ArrayList<>();
        for (Node n : nodes) {
            if (n.isRoot()){
                root.add(n);
            }
        }
        return root;
    }

    /**
     * 设置图标
     * @param n
     */
    private static void setNodeIcon(Node n) {

        if (n.getChildren().size() > 0 && n.isExpand()){
            n.setIcon(R.mipmap.tree_ex);
        }else if (n.getChildren().size() > 0 && !n.isExpand()){
            n.setIcon(R.mipmap.tree_ec);
        }else {
            n.setIcon(-1);
        }
    }




}
